<?php

class PengirimanModel{
	private $db;
	private $subbagian;
	
	private $validColumn;
	private $data;

	private $result;

	private $sort_by;
	private $sort_order;

	private $search_keyword;
	private $search_field;

	private $limit;
	
	function PengirimanModel($dbObject){
		$this->subbagian 	= 'pengiriman';		
		$this->db 			= $dbObject;

		$this->validColumn  = array(
			'tgl_pengiriman', 
			'po_id', 
			'no_do',
			'pelanggan_id',
			'tujuan',
			
			'jml_kotak',
			'jml_kecambah',
			'keterangan'
			);
		
		// settting up configuration
		if(($this->start=$this->getGET('start')) == NULL)
			$this->start = 0;

		$this->sort_by 			= $this->getGET('sort_by');
		$this->sort_order 		= $this->getGET('sort_order');
		$this->search_keyword	= $this->getGET('search_keyword');
		$this->search_field		= $this->getGET('search_field');

		if(($this->limit=$this->getGET('limit')) == NULL)
			$this->limit = 50;					
	}
	
	private function getGET($param){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return NULL;
	}
    
    public function getPelangganList(){
        $q = 'SELECT `Id` FROM `caramel_managerial`.`pelanggan`';
        return $this->db->get_results($q, ARRAY_A);
    }
    
    public function getPOList(){
        $q = 'SELECT `Id` FROM `caramel_managerial`.`po`';
        return $this->db->get_results($q, ARRAY_A);
    }
	
	public function countAll(){
		$where = $this->prepareWhereClause();	
			
		$q = "SELECT COUNT(*) FROM `caramel_managerial`.`pengiriman` $where";
		
		$this->total_record = $this->db->get_var($q);
		
		return $this->total_record;
	}	
	
	private function prepareWhereClause(){
		$where = '';
		if( $this->search_keyword && $this->search_field ){
			switch($this->search_field){
					case 'pengiriman_item':
						$where = "WHERE `id` IN (SELECT `pengiriman_id` FROM `caramel_managerial`.`pengiriman_item` WHERE `jml_kotak`='".$this->search_keyword."')";
						break;
					default:
						$where = "WHERE ".$this->search_field."='".$this->search_keyword."'";
			}
		}
		
		return $where;
	}
	
	private function getDetailPengiriman($id, $detail){
		$q = '';
		switch($detail){
			case 'pengiriman_item':
				$q = "SELECT `jml_kotak`, `jml_kecambah`, `keterangan` FROM `caramel_managerial`.`pengiriman_item` WHERE `pengiriman_id`='".$id."'";
				break;			
		}
		$result = array();
		$r = $this->db->get_results($q, ARRAY_A);
		if( $r )	
			foreach($r as $j) $result[] = $j;
		
		return $result;
	}
	
	public function search(){			
		$where = $this->prepareWhereClause();
		
		// get data
		if( $this->sort_by )
			$q = "
			SELECT `id`, `tgl_pengiriman`, `po_id`, `no_do`, `pelanggan_id`, `tujuan`
			FROM  `caramel_managerial`.`pengiriman` $where
			ORDER BY ".$this->sort_by." ".$this->sort_order." LIMIT ".$this->start.",".$this->limit."";
		else
			$q = "
			SELECT `id`, `tgl_pengiriman`, `po_id`, `no_do`, `pelanggan_id`, `tujuan`
			FROM  `caramel_managerial`.`pengiriman` $where
			LIMIT ".$this->start.",".$this->limit."";

		$this->result = $this->db->get_results($q, ARRAY_A);
		// detail
		if( count($this->result) > 0 ){
			foreach($this->result as &$r){
				$r['pengiriman_item'] = $this->getDetailPengiriman($r['id'], 'pengiriman_item');
			}
		}
		return $this->result;
	}

	public function getResult(){
		return $this->result;
	}
	
	function format($data){
		$result = array();
		foreach($this->validColumn as $v){
			if( isset($data[$v]) )
				$result[$v] = $data[$v];
			else
				$result[$v] = NULL;
		}
		
		return $result;
	}
	
	private function insertPengiriman(){
		$dataPengiriman   = array();
		$pengirimanColumn = array(
			'tgl_pengiriman', 
			'po_id', 
			'no_do',
			'pelanggan_id',
			'tujuan',
		);
		
		// copy dari $data ke $dataPengiriman
		foreach($pengirimanColumn as $c){
			$dataPengiriman[$c] = $this->data[$c];
		}
		
		$qHeader = "(`".implode($pengirimanColumn, "`, `")."`)";
		$qBody   = "('".implode($dataPengiriman, "', '")."')";
		
		$query = "INSERT INTO `caramel_managerial`.`pengiriman` ".$qHeader." VALUES ".$qBody;
		$this->db->query($query);
		
		$id = $this->db->get_var("SELECT MAX(`Id`) FROM `caramel_managerial`.`pengiriman`");
		
		return $id;
	}
	
	private function insertOrderDO($id){
		$dataOrderDO = array();
		$orderDOColumn = array(			
			'jml_kotak',
			'jml_kecambah',
			'keterangan'
		);
		
		// copy dari $data ke $dataOrderDO
		foreach($orderDOColumn as $c){
			$dataOrderDO[$c] = $this->data[$c];
		}
		
		if( count($dataOrderDO['jml_kotak']) > 0 ){
		
			$qHeader = "(`pengiriman_id`, `jml_kotak`, `jml_kecambah`, `keterangan`)";			
			$query   = "INSERT INTO `caramel_managerial`.`pengiriman_item` ".$qHeader." VALUES ";
			$temp    = '';
			
			// looping through all data
			for($i=0; $i<count($dataOrderDO['jml_kotak']); $i++){			
				$qBody   = "('".$id."', '".$dataOrderDO['jml_kotak'][$i]."', '".$dataOrderDO['jml_kecambah'][$i]."', '".$dataOrderDO['keterangan'][$i]."')";
				$temp[]  = $qBody;
			}
			
			$query .= implode($temp, ",").';';
			
			$this->db->query($query);
		}
	}
	
	
	function put($data){		
		$this->data	= $this->format($data);// buang field2 yang tidak dibutuhkan
		
		if( $this->isExist() == FALSE ){				
			// bagging
			$id = $this->insertPengiriman();
			// tandan
			$this->insertOrderDO($id);					
		}
		else{
			return INSERT_FAIL_DUPLICATE;
		}		
	}
	
	// multiple
	function isExist(){			
		$query = "SELECT COUNT(*) FROM `caramel_managerial`.`pengiriman` WHERE `no_do`='".$this->data['no_do']."'";	
		$count = $this->db->get_var($query);
		
		if( $count > 0 )
			return TRUE;	
			
		return FALSE; // tidak ada data yang dicari... no duplicate		
	}
    
    public function delete($id){
        $q[] = "
        DELETE FROM `caramel_managerial`.`pengiriman` 
        WHERE `id`='$id'";
        
        $q[] = "
        DELETE FROM `caramel_managerial`.`pengiriman_item` 
        WHERE `pengiriman_id`='$id'";
        
        foreach($q as $query){
            $this->db->query($query);
        }
    }
}
?>